package com.cskaoyan;

import com.cskaoyan.dao.UserDao;
import com.cskaoyan.utils.MybatisUtils;
import com.cskaoyan.vo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

/**
 * Mybatis 输入参数
 */
public class MybatisTestParam {


    /**
     *     传一个简单参数
     *     什么是简单参数呢？基本类型+ String是简单参数
     *
     *     注意在写sql语句的时候，#{任意字符}
     */
    @Test
    public void testSelectSimpleParam(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User user = mapper.selectUserByName("张飞");

        System.out.println(user);

    }

/** 传入多个参数 */

     /**
     * 1. 使用@Param注解，注意一定要使用这个注解，不然会报错
     */
    @Test
    public void testSelectMultiParam(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        List<User> users = mapper.selectListByNameOrAge("张飞", 28);

        users.forEach(user -> System.out.println(user));

    }

    /**
     * 2. 使用对象传参
     *
     * 使用单个对象传递参数时
     *  接口中假如不写@Param注解的话，那么在取值的时候直接#{属性值}即可
     *  接口中假如写@Param注解的话，例如
     *      User selectListByUser(@Param(value="user") User user);
     *      那么在取值的时候就需要加上@Param注解里面的值 如： #{user.name}
     */
    @Test
    public void testSelectParamByObject(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setName("张飞");
        user.setAge(28);
        List<User> users = mapper.selectListByUser(user);

        users.forEach(user1 -> System.out.println(user1));

    }

    /**
     * 测试使用对象传参插入
     */
    @Test
    public void testInsert(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setId(1005);
        user.setName("马超");
        user.setEmail("ma@alibaba.com");
        user.setAge(60);
        Integer effectedRows = mapper.insertUser(user);
        sqlSession.commit();

        System.out.println(effectedRows);

    }

    /**
     * 使用Map传参
     *
     * 获取参数的值的时候，需要使用 #{map中的key} 来获取值
     */
    @Test
    public void testSelectByMap(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name","马超");
        hashMap.put("age",20);
        List<User> list = mapper.selectListByMap(hashMap);

        list.forEach(user -> System.out.println(user));
    }

    /**
     * 混合传参
     *
     * 混合传参必须各个参数加上@Param注解才有效
     * map取值 #{mapName.key}
     * PoJo取值 #{beanName.field}
     */
    @Test
    public void testSelectMixParam(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name","张飞");

        User user = new User();
        user.setAge(30);
        List<User> userList = userDao.selectListByMixedParam(1001, hashMap, user);

        userList.forEach(user1 -> System.out.println(user1));

    }


    /**
     * 位置传值
     */
    @Test
    public void testSelectOneByIndex(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<User> list = userDao.selectListByIndex("张飞", 20);
        list.forEach(user -> System.out.println(user));

        sqlSession.close();


    }

    @Test
    public void testSelectOneByIndex2(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<User> list = userDao.selectListByIndex2("张飞", 20);
        list.forEach(user -> System.out.println(user));

        sqlSession.close();


    }

    @Test
    public void selectListBy$(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setAge(18);
        String orderBy = "age";
        List<User> users = userDao.selectBy$("user",user,orderBy);

        users.forEach(user1 -> System.out.println(user1));
    }


}
